package com.hopoke.common.domain;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;

@Data
public class PageQuery {

    private Integer pageNum = 1;

    private Integer pageSize = 10;

    private String sortBy;

    private Boolean isAsc = true;

    public <T> Page<T> toMpPage(OrderItem ... items) {
        Page<T> page = Page.of(pageNum, pageSize);
        if (!sortBy.isBlank()) {
            page.addOrder(new OrderItem(sortBy,isAsc));
        } else if (items != null){
            page.addOrder(items);
        }
        return page;
    }

    public <T> Page<T> toMpPage(String defaultSortBy,Boolean isAsc) {
        Page<T> page = Page.of(pageNum, pageSize);
        page.addOrder(new OrderItem(defaultSortBy,isAsc));
        return page;
    }

    public <T> Page<T> toMpPageDefaultSortByCreateTime() {
        return toMpPage("create_time", false);
    }

    public <T> Page<T> toMpPageDefaultSortByUpdateTime() {
        return toMpPage("update_time", false);
    }
}
